package com.tedu.controller;

import com.tedu.entry.Dept;
import com.tedu.service.impl.DeptServiceImpl;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：毕龙
 * Date: 2023/7/28 10:28
 */
@WebServlet("/dept/findAll")
public class DeptAllController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.处理请求

        //处理响应的中文乱码
        response.setCharacterEncoding("utf-8");

        //2.调用业务逻辑
        DeptServiceImpl service = new DeptServiceImpl();
        List<Dept> depts = service.selectAllDept();


        //将结果转化为json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("depts", depts);
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());

    }
}
